import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import type { ShoppingCart, DistributionAddress } from '../config/data/data'

export const useGoodsStore = defineStore('goodsStore', () => {
  //购物车数据
  const shoppingCart = ref<ShoppingCart[]>([])

  //默认/当前地址
  const defaultAddress = ref<DistributionAddress>({
    address_id: '1231221',
    address_city: '玉林市',
    address_province: '广西',
    address_county: '博白县',
    address_town: '宁潭镇', //配送镇
    address_street: '长春村老园队对018号', //配送地址街道
    address_username: '李某某', //收货人
    address_phone: '123132131',
  })

  //过滤--计算属性
  const getShoppingCart = computed(() => {
    return shoppingCart.value.reverse()
  })

  // 购物车新增
  const shoppingCartAdd = (item: ShoppingCart) => {
    shoppingCart.value.unshift(item)
  }

  //购物车减少
  const shoppingCartDel = (index: number) => {
    shoppingCart.value.splice(index, 1)
  }

  return { shoppingCart, shoppingCartDel, shoppingCartAdd,defaultAddress }
})
